﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RemoteDesktops.Classes
{
  public class TokenManager 
  {
    private static Dictionary<int, Guid> _dictionary; 
    private static object _lockCtx; 

    /// <summary>
    /// 
    /// </summary>
    static TokenManager()
    {
      _lockCtx = new object();
      _dictionary = new Dictionary<int, Guid>();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void Add(int key, Guid value)
    {
      lock (_lockCtx)
      {
        _dictionary.Add(key, value);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    public static void Add(Guid value)
    {
      lock (_lockCtx)
      {
        _dictionary.Add(_dictionary.Count, value);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static Guid Value(int key)
    {
      return _dictionary[key];
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="lookup"></param>
    /// <returns></returns>
    public static int KeyValue(Guid lookup)
    {
      KeyValuePair<int,Guid> r = _dictionary.Where(ex => ex.Value == lookup).FirstOrDefault();
      if (r.Value.Equals(lookup))
        return r.Key;
      return -1; 
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    public static void Remove(int key)
    {
      lock (_lockCtx)
      {
        _dictionary.Remove(key);
      }      
    }

    /// <summary>
    /// 
    /// </summary>
    public static int Count
    {
      get
      {
        return _dictionary.Count;
      }
    }
  }
}
